//
//  DownView.m
//  A段项目
//
//  Created by dlios on 15/7/14.
//  Copyright (c) 2015年 dlios. All rights reserved.
//

#import "DownView.h"
#import "BaseHeader.h"
#import "DowanTableViewCell.h"


@implementation DownView

- (void)dealloc
{
    [_upArr release];
    [_downArr release];
    [_table release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.upArr = [NSMutableArray array];
        self.downArr = [NSMutableArray array];
        self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, frame.size.height) style:UITableViewStylePlain];
        _table.dataSource = self;
        _table.delegate = self;
        [_table registerClass:[DowanTableViewCell class] forCellReuseIdentifier:@"c"];
        [self addSubview:_table];
        [_table release];
        

    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DowanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"c"];
    if (indexPath.row == 0) {
        [self createButton:self.upArr cell:cell];
        cell.title.text = @"已订阅的标签/点击删除";
        cell.title.textColor = [UIColor colorWithRed:30 / 255.0 green:144 / 255.0 blue:1 alpha:0.6];


    } else
    {
        cell.title.text = @"点击订阅更多标签";
        [self createButton:self.downArr cell:cell];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        
        return  (self.upArr.count / 4 + 2) * 45;
    } else
    {
        return (self.downArr.count / 4 + 2) * 45;
    }
}


- (void)createButton:(NSMutableArray *)array cell:(DowanTableViewCell *)cell
{
    NSArray *arr = cell.subviews;
    for (UIButton *button in arr) {
        if ([button isKindOfClass:[UIButton class]]) {
            
            [button removeFromSuperview];
        }
    }
    
    int i = 0;
    for (NSDictionary *dic in array) {
        UIButton *aButton = [[UIButton alloc] initWithFrame:CGRectMake(i % 4 * 90 + 10, i / 4 * 45 + 35, 80, 35)];
        aButton.tag = i + 1;
        aButton.layer.cornerRadius = 10;
        aButton.layer.masksToBounds = YES;
        [aButton setTitle:[dic objectForKey:@"title"] forState:UIControlStateNormal];
        [aButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        aButton.titleLabel.font = [UIFont systemFontOfSize:15];
        aButton.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:0.8];
//        aButton.layer.borderColor = [UIColor colorWithRed:30 / 255.0 green:144 / 255.0 blue:1 alpha:0.6].CGColor;
//        aButton.layer.borderWidth = 1;
        [aButton addTarget:self action:@selector(touchAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:aButton];
        [aButton release];
        i++;
        
    }
    
}


- (void)touchAction:(id)sender
{
    UIButton *button = (UIButton *)sender;
    NSString *str = button.titleLabel.text;
    NSMutableArray *array = [NSMutableArray arrayWithArray:_upArr];
    [array addObjectsFromArray:_downArr];
    NSInteger up = _upArr.count;
    int i = 1;
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    for (dic in array) {
        
        NSString *str1 = [dic objectForKey:@"title"];
        if ([str isEqualToString:str1]) {
           
            break;
        }
        i++;
    }
    if (i > up) {
        [self.upArr addObject:dic];
        [self.downArr removeObject:dic];
    } else
    {
        [self.downArr addObject:dic];
        [self.upArr removeObject:dic];
    }
    [self.table reloadData];
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:_upArr forKey:@"cancell"];
    [defaults setObject:_downArr forKey:@"add"];
        
}







/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
